package com.java.agent.enhance.definer;

import com.java.agent.enhance.BaseDefiner;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.dynamic.DynamicType;
import net.bytebuddy.implementation.MethodDelegation;
import net.bytebuddy.utility.JavaModule;

import java.lang.reflect.InvocationTargetException;

import static net.bytebuddy.matcher.ElementMatchers.named;

public class ServlaterDefiner extends BaseDefiner {

    private static String className = "org.springframework.web.servlet.DispatcherServlet";
    public ServlaterDefiner() {
        super(className);
    }

    public  String getClassName(){
        return className;
    }

    public  String getMentodName(){
        return "doDispatch";
    }

    @Override
    public String getAnnotation() {
        return null;
    }

    @Override
    public String getEnhancedClass() {
        return "com.java.agent.enhance.servlate.ServlateEnhance";
    }

    @Override
    public DynamicType.Builder<?> enhanced(DynamicType.Builder<?> builder, TypeDescription typeDescription, ClassLoader classLoader, JavaModule module) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
        return builder.method(named( getMentodName())).intercept(MethodDelegation.to(Class.forName(getEnhancedClass()).getDeclaredConstructor().newInstance()));
    }

}
